package com.study.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.exception.ApiException;
import com.study.common.utils.PubFinalData;
import com.study.common.utils.StringUtils;
import com.study.modules.sys.mapper.SysRoleMapper;
import com.study.modules.sys.mapper.SysRoleMenuMapper;
import com.study.modules.sys.mapper.SysUserRoleMapper;
import com.study.modules.sys.model.dto.SysRoleDTO;
import com.study.modules.sys.model.entity.SysMenu;
import com.study.modules.sys.model.entity.SysRole;
import com.study.modules.sys.model.entity.SysRoleMenu;
import com.study.modules.sys.service.SysRoleMenuService;
import com.study.modules.sys.service.SysRoleService;
import com.study.modules.sys.service.SysUserRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author Mr.Du
 * @Date 2021/10/16 10:49
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysUserRoleService userRoleService;


    @Override
    public IPage<SysRole> queryRoleList(Page<SysRole> page) {
        return baseMapper.queryRoleList(page);
    }

    @Override
    public SysRoleDTO selectRoleInfo(Long roleId) {
        SysRole role = baseMapper.selectRoleById(roleId);
        if (role == null) throw new ApiException("角色不存在，无法查看！");
        SysRoleDTO roleDTO = new SysRoleDTO();
        BeanUtils.copyProperties(role, roleDTO);
        //根据角色id查询绑定的菜单列表
        List<SysMenu> menuList = roleMenuService.selectMenuListByRoleId(roleId);
        if (menuList != null && menuList.size() > 0) {
            roleDTO.setMenuList(menuList);
        }
        return roleDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(SysRoleDTO sysRole, Long userId) {
        SysRole dbRole = baseMapper.selectOne(new QueryWrapper<SysRole>().eq("name", sysRole.getName()));
        if (dbRole != null) throw new ApiException("角色名不能重复，请输入新的角色名！");
        SysRole sysRole1 = new SysRole();
        BeanUtils.copyProperties(sysRole, sysRole1);
        sysRole1.setDeleted(PubFinalData.UN_DELETED);
        sysRole1.setUpdateId(userId);
        sysRole1.setCreateTime(new Date());
        baseMapper.insert(sysRole1);
        List<Long> MenuList = StringUtils.covertListByStr(sysRole.getMenuIdList());
        if (MenuList != null && MenuList.size() > 0) {
            SysRole role = baseMapper.selectOne(new QueryWrapper<SysRole>().eq("name", sysRole.getName()));
            for (Long menuId : MenuList) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(role.getId());
                roleMenuMapper.insert(roleMenu);
            }
        }
        return true;
    }

    @Override
    public Boolean updateRole(SysRoleDTO sysRole, Long userId) {
        SysRole role = baseMapper.selectRoleById(sysRole.getId());
        if (role == null) throw new ApiException("角色信息不存在，无法更新！");
        SysRole role1 = new SysRole();
        BeanUtils.copyProperties(sysRole, role1);
        baseMapper.updateById(role1);
        List<Long> menuIdList = StringUtils.covertListByStr(sysRole.getMenuIdList());
        roleMenuService.deleteRoleMenuByRoleId(role.getId());
        roleMenuService.updateRoleMenu(role.getId(),menuIdList);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long roleId, Long userId) {
        SysRole role = baseMapper.selectRoleById(roleId);
        if (role == null) throw new ApiException("角色不存在，无法删除！");
        //删除用户角色关联
        userRoleService.deleteUserRoleByRoleId(roleId);
        //删除角色菜单关联
        roleMenuService.deleteRoleMenuByRoleId(roleId);
        //删除角色
        role.setDeleted(PubFinalData.DELETED);
        role.setUpdateId(userId);
        role.setUpdateTime(new Date());
        baseMapper.updateById(role);
        return true;
    }

}
